package com.szh.mysobproject.ui.fragment

import android.graphics.Color
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.bumptech.glide.Glide
import com.szh.mysobproject.R
import com.szh.mysobproject.action.StatusAction
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.app.TitleBarFragment
import com.szh.mysobproject.databinding.DiscoverFragmentBinding
import com.szh.mysobproject.http.response.model.WallpaperBannerBean
import com.szh.mysobproject.other.GridSpaceDecoration
import com.szh.mysobproject.ui.activity.GalleryActivity
import com.szh.mysobproject.ui.adapter.AdapterDelegate
import com.szh.mysobproject.ui.adapter.WallpaperListAdapter
import com.szh.mysobproject.util.CustomAnimation
import com.szh.mysobproject.util.dp
import com.szh.mysobproject.util.loadStateListener
import com.szh.mysobproject.viewmodel.PhotoViewModel
import com.szh.mysobproject.viewmodel.app.Repository
import com.szh.mysobproject.widget.StatusLayout
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.coroutines.flow.collectLatest


class DiscoverFragment : TitleBarFragment<AppActivity>(), StatusAction {

    private val mBinding by viewBinding<DiscoverFragmentBinding>()
    private val mPhotoViewModel by viewModels<PhotoViewModel>()
    private val mWallpaperBannerAdapter = BannerAdapter()
    private val mWallPaperListAdapter = WallpaperListAdapter(AdapterDelegate().apply {
        //recyclerView item 动画
        adapterAnimation = CustomAnimation()
    })
    private val loadStateListener = loadStateListener(mWallPaperListAdapter) {
        mBinding.refreshLayout.finishRefresh()
    }


    override fun getLayoutId(): Int = R.layout.discover_fragment

    override fun initObserver() {
        //获取壁纸轮播图
        loadWallpaperBannerList()
    }

    private fun loadWallpaperBannerList() {
        mPhotoViewModel.getWallpaperBannerList().observe(viewLifecycleOwner) {
            val slBannerHint = mBinding.slBannerHint
            val wallpaperBannerList = it.getOrElse {
                slBannerHint.showError {
                    loadWallpaperBannerList()
                }
                return@observe
            }
            mWallpaperBannerAdapter.setDatas(wallpaperBannerList)
            if (wallpaperBannerList.isEmpty()) slBannerHint.showEmpty() else slBannerHint.showComplete()
        }
    }

    override fun initEvent() {
        mBinding.refreshLayout.setOnRefreshListener {
            mWallPaperListAdapter.refresh()
        }
        mWallPaperListAdapter.addLoadStateListener(loadStateListener)
        mWallPaperListAdapter.setOnItemClickListener { view, verticalPhoto, _ ->
            Repository.setPhotoIdList(mWallPaperListAdapter.snapshot().items.toList())
            GalleryActivity.smoothEntry(requireActivity(), verticalPhoto.id, view)
        }
    }

    override fun initData() {
        //获取壁纸列表
        loadPhotoList()
    }

    private fun loadPhotoList() {
        viewLifecycleOwner.lifecycleScope.launchWhenCreated {
            mPhotoViewModel.getWallpaperList().collectLatest {
                mWallPaperListAdapter.submitData(it)
            }
        }
    }

    override fun initView() {
        mBinding.banner.apply {
            addBannerLifecycleObserver(viewLifecycleOwner)
            setAdapter(mWallpaperBannerAdapter)
            indicator = CircleIndicator(requireContext())
            setIndicatorSelectedColor(Color.WHITE)
        }
        mBinding.rvPhotoList.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = mWallPaperListAdapter
            addItemDecoration(GridSpaceDecoration(4.dp))
        }
    }

    override fun getStatusLayout(): StatusLayout = mBinding.slDiscoverHint

    override fun isStatusBarEnabled(): Boolean {
        return !super.isStatusBarEnabled()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mWallPaperListAdapter.removeLoadStateListener(loadStateListener)
    }

    companion object {
        private class BannerAdapter : BannerImageAdapter<WallpaperBannerBean.Data>(null) {
            override fun onBindView(
                holder: BannerImageHolder,
                data: WallpaperBannerBean.Data,
                position: Int,
                size: Int
            ) {
                Glide.with(holder.itemView)
                    .load(data.urlThumb)
                    .into(holder.imageView)
            }
        }

        @JvmStatic
        fun newInstance() = DiscoverFragment()
    }
}